package io.scnb.lesson002.demo19;

import io.scnb.lesson002.demo19.test1.MainConfig1;
import io.scnb.lesson002.demo19.test2.MainConfig2;
import io.scnb.lesson002.demo19.test3.MainConfig3;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class MessageSourceTest {
    
    @Test
    public void test1() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(MainConfig1.class);
        context.refresh();
        // 未指定Locale，此时系统会使用默认的Locale对象，本地默认的值为中文【中国】，即zh_CN
        System.out.println(context.getMessage("name", null, null));
        System.out.println(context.getMessage("personal_introduction", null, null));
        System.out.println(context.getMessage("name", null, Locale.CHINA));
        System.out.println(context.getMessage("personal_introduction", null, Locale.CHINA));
        System.out.println(context.getMessage("name", null, Locale.UK));
        System.out.println(context.getMessage("personal_introduction", null, Locale.UK));
    }
    
    @Test
    public void test2() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(MainConfig1.class);
        context.refresh();

        System.out.println(context.getMessage("personal_introduction", new String[]{"spring高手", "java高手"}, Locale.CHINA));
        System.out.println(context.getMessage("personal_introduction", new String[]{"spring", "java"}, Locale.UK));
    }
    
    @Test
    public void test3() throws InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(MainConfig2.class);
        context.refresh();
        
        // 输出2次
        for (int i = 0; i < 3; i++) {
            System.out.println(context.getMessage("address", null, Locale.CHINA));
            TimeUnit.SECONDS.sleep(8);
        }
    }
    
    @Test
    public void test4() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(MainConfig3.class);
        context.refresh();

        System.out.println(context.getMessage("desc", null, Locale.CHINA));
        System.out.println(context.getMessage("desc", null, Locale.UK));
    }
}
